package com.gitee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.bo.UserDetailsBo;
import com.gitee.mapper.UserMapper;
import com.gitee.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Objects;

/**
 * @description: TODO
 * @author: chennl
 * @date: 2021/12/30
 * @version: 1.0
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户名加载用户信息
     *  后续可以将用户信息缓存起来；每次获取用户信息时，优先从缓存中取；缓存没有的时候，再去数据库中取
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetailsBo loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq(User.USERNAME, username));
        if (Objects.isNull(user)) {
            return null;
        }
        // TODO 查询用户对应的访问权限

        return new UserDetailsBo(user, Collections.emptyList());
    }
}
